package org.bluedream.core.module.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.core.config.advice.RsaSecurityParameter;
import org.bluedream.core.module.sys.entity.Role;
import org.bluedream.core.module.sys.entity.User;
import org.bluedream.core.module.sys.service.RoleService;
import org.bluedream.core.module.sys.service.UserService;
import org.bluedream.core.utils.UserUtil;
import org.bluedream.core.utils.configUtil.GoalConfigUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("${adminPath}/role")
public class RoleController extends BaseController {
    @Resource
    private RoleService roleService;

    @Resource
    private UserService userService;

    @ModelAttribute
    public Role get(String id){
        return roleService.getById(id);
    }

    @RequiresPermissions("role:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(Role role, Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("role" , role);
        return "sys/role/roleList";
    }

    @RequiresPermissions("role:view")
    @RequestMapping("/form")
    public String form(Role role, Model model){
        role = roleService.createNextNode(role);
        model.addAttribute("role" , role);
        return "sys/role/roleForm";
    }

    @RequiresPermissions("role:view")
    @RequestMapping("/orgForm")
    public String orgForm(Role role, Model model){
        model.addAttribute("role" , role);
        return "sys/role/roleOrgForm";
    }

    @RequiresPermissions("role:view")
    @RequestMapping("/menuForm")
    public String menuForm(Role role, Model model){
        model.addAttribute("role" , role);
        return "sys/role/roleMenuForm";
    }

    @RequiresPermissions("role:view")
    @RequestMapping("/userForm")
    public String userForm(Role role, Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("role" , role);
        model.addAttribute("user" , new User());
        return "sys/role/roleUserSelect";
    }

    @RequiresPermissions("role:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<Role> findPage(@RsaSecurityParameter Role role){
        return roleService.findPage(role , "a.role_code");
    }

    @RequiresPermissions("role:view")
    @RequestMapping("/findPage4User")
    @ResponseBody
    public IPage<User> findPage4User(@RsaSecurityParameter User user , @RsaSecurityParameter String searchId){
        return userService.findPageChecked(user , searchId , "role");
    }

    @RequiresPermissions("role:view")
    @RequestMapping("/treeDataToOrg")
    @ResponseBody
    public List<Map<String , Object>> treeDataToOrg(@RequestParam(defaultValue="1")String isShowCode , @RequestParam(defaultValue="undefined")String excludeCode , Role role){
        return roleService.treeDataToOrg(isShowCode , excludeCode , role);
    }

    @RequiresPermissions("role:view")
    @RequestMapping("/treeDataToMenu")
    @ResponseBody
    public List<Map<String , Object>> treeDataToMenu(@RequestParam(defaultValue="1")String isShowCode , @RequestParam(defaultValue="undefined")String excludeCode , Role role){
        return roleService.treeDataToMenu(isShowCode , excludeCode , role);
    }

    @RequiresPermissions("role:edit")
    @RequestMapping("/save")
    @ResponseBody
    public String save(Role role){
        boolean flag = roleService.save(role);
        if (flag){
            return "角色信息保存成功";
        }else {
            return "角色信息保存失败";
        }
    }

    @RequestMapping("/roleRelationSave")
    @ResponseBody
    public String roleRelationSave(String id , String type , Role role ){
        role.setId(id);
        String msg;
        if (type.equals("org")){
            boolean flag = roleService.saveOrg(role);
            if (flag){
                msg = "角色-组织信息保存成功";
            }else {
                msg = "角色-组织信息保存失败";
            }
        }else if (type.equals("menu")){
            boolean flag = roleService.saveMenu(role);
            if (flag){
                msg = "角色-权限信息保存成功";
            }else {
                msg = "角色-权限信息保存失败";
            }
        }else if (type.equals("user")){
            boolean flag = roleService.saveUser(role);
            if (flag){
                msg = "角色-用户授权成功";
            }else {
                msg = "角色-用户授权失败";
            }
        }else {
            msg = "";
        }
        return msg;
    }

}
